package com.ruoyi.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.business.entity.YytMember;
import com.ruoyi.common.business.service.YytMemberService;
import com.ruoyi.common.pms.entity.YytPmsSkuInfo;
import com.ruoyi.common.pms.entity.YytPmsSpuInfo;
import com.ruoyi.common.pms.service.YytPmsSpuInfoService;
import com.ruoyi.common.sms.entity.YytSmsCouponHistory;
import com.ruoyi.common.sms.service.YytSmsCouponHistoryService;
import com.ruoyi.common.sms.service.YytSmsCouponService;
import com.ruoyi.utils.LoginUtils;
import com.ruoyi.utils.RedisService;
import com.ruoyi.utils.ResponseResult;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
* <p>
    * 优惠券领取历史记录 前端控制器
    * </p>
*
* @author MrPing
* @since 2022-05-31
*/
@RestController
@RequestMapping("/history")
public class YytSmsCouponHistoryController {

    @Autowired
    private RedisService redisService;

    @Autowired
    private YytSmsCouponService yytSmsCouponService;

    @Autowired
    private YytSmsCouponHistoryService yytSmsCouponHistoryService;

    @Autowired
    private YytPmsSpuInfoService yytPmsSpuInfoService;

    @Autowired
    private YytMemberService yytMemberService;

    /**
     * 用户购买商品获取用户是否拥有该商品优惠券
     * @param memberId
     * @param spuId
     * @return
     */
    @GetMapping("/getHistoryByUserSpuId")
    public ResponseResult getHistoryByUserSpuId(@RequestParam("memberId") String memberId, @RequestParam("spuId") String spuId){
        return ResponseResult.success(yytSmsCouponHistoryService.getUserCouponSpuList(memberId, spuId));
    }

    /**
     * 取消订单改优惠券占用状态
     * @param ids
     * @return
     */
    @GetMapping("/upCouponMode")
    public ResponseResult upCouponMode(@RequestParam("ids") String ids, @RequestParam("orderId") String orderId){
        return ResponseResult.success(yytSmsCouponHistoryService.upCouponModeStatus(ids, orderId));
    }


    @GetMapping("/getUserCoupon")
    public ResponseResult getUserCoupon(){
        YytMember loginUser = LoginUtils.getLoginUser(redisService);

        // 添加新用户优惠券
        return ResponseResult.success(yytSmsCouponService.addCoupon(loginUser));
    }


    @GetMapping("/getMyCouponList")
    public ResponseResult getMyCouponList(@RequestParam("openId") String openId) {
        List<String> list = new ArrayList<>();
        list.add("1569985866041880577");
        list.add("1569985596788535298");
        list.add("1569985087897825281");
        list.add("1569966862833250306");
        QueryWrapper<YytMember> wrapper = new QueryWrapper<>();
        wrapper.eq("wechat_id", openId).eq("del_flag", 0);
        List<YytMember> yytMembers = yytMemberService.list(wrapper);
        if(yytMembers.size() == 0){
            return ResponseResult.success(false);
        }
        for (YytMember yytMember : yytMembers) {
            QueryWrapper<YytSmsCouponHistory> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("member_id", yytMember.getId()).in("coupon_id", list);
            List<YytSmsCouponHistory> histories = yytSmsCouponHistoryService.list(queryWrapper);
            if (histories.size() > 0) return ResponseResult.success(true);
        }
        return ResponseResult.success(false);
    }

}
